import {baseUrl} from "./globalConf";
import axios from "axios";
import store from '../store'
import {Message} from "element-ui"
import router from "../router";
axios.defaults.baseURL = baseUrl
axios.defaults.timeout = 10000;


//前置攔截
axios.interceptors.request.use(config => {
    const token = localStorage.getItem('token');
    token && (config.headers.Authorization = token);
    return config
},error => {
    console.log(error)
})
//后置拦截
axios.interceptors.response.use(res => {
    if (res.data != null && res.data.status === 401){
        Message({
            message: '请先登录',
            type: 'warning'
        });
        setTimeout(()=>{
            router.push({
                name:'Login'
            })
        },1000)
        return Promise.reject(res)
    }
            return res.data;
    },
    error => {
            return Promise.reject(error)
        }
)

